package com.ff.neuropp.service;


import com.ff.neuropp.domain.CareerType;
import com.ff.neuropp.repository.CareerTypeRepository;
import com.ff.neuropp.util.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class CareerTypeService {

    @Resource
    private CareerTypeRepository careerTypeDao;


    /**
     * 创建实体
     *
     * @param careerType
     */
    public CareerType saveCareerType(CareerType careerType) {
        return careerTypeDao.save(careerType);
    }

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public CareerType findById(Integer id) {
        Optional<CareerType> optional = careerTypeDao.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    /**
     * 根据id 删除实体
     * @param id
     */
    public void deleteById(Integer id) {
        careerTypeDao.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param careerType
     * @return
     */
    public Page<CareerType> findPage(CareerType careerType) {
        Map<String, Object> sqlParams = new HashMap<>();
        if (null == careerType.getPageNo()) {
            careerType.setPageNo(1);
        } else if (careerType.getPageNo() < 1) {
            careerType.setPageNo(1);
        }
        if (null == careerType.getPageSize()) {
            careerType.setPageSize(10);
        }
        sqlParams.put("GT_id",0);
        if(null !=careerType.getSearch() && careerType.getSearch().trim().length()>0){
            sqlParams.put("LIKE_name",careerType.getSearch());
        }
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(careerType.getPageNo()-1,careerType.getPageSize(), direction, sort);
        return careerTypeDao.findAll(SpecificationUtil.buildSpecification(sqlParams, CareerType.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<CareerType> findList(Map<String, Object> param) {
        return careerTypeDao.findAll(SpecificationUtil.buildSpecification(param, CareerType.class));
    }



}
